<?php

/**
 * This is the model class for table "customerquote".
 *
 * The followings are the available columns in table 'customerquote':
 * @property integer $id
 * @property integer $customerId
 * @property integer $quoteId
 * @property string $quoteDetail
 * @property double $price
 * @property string $status
 * @property string $createTime
 * @property string $modifyTime
 * @property string $paymentMethod
 * @property string $paymentStatus
 * @property string $incomingLabelCarrier
 * @property string $incomingLabelTrackingCode
 * @property string $outgoingLabelCarrier
 * @property string $outgoingLabelTrackingCode
 *
 * The followings are the available model relations:
 * @property Gadgetquote $quote
 * @property Customer $customer
 * @property Customerquotelog[] $customerquotelogs
 * @property Paymentcheck[] $paymentchecks
 * @property Paymentpaypal[] $paymentpaypals
 */
class Customerquote extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Customerquote the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	const ARRSTATUSW = 'Waiting';
	const ARRSTATUSR = 'Reject';
	const ARRSTATUSA = 'Accept';
	const ARRSTATUSC = 'Completed';
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'customerquote';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			// array('customerId, quoteId, quoteDetail, price, status, createTime, paymentMethod', 'required'),
			array('customerId, quoteId,  price, status, createTime, paymentMethod', 'required'),
			array('customerId, quoteId', 'numerical', 'integerOnly'=>true),
			array('price', 'numerical'),
			array('quoteDetail', 'length', 'max'=>255),
			array('status, paymentStatus, incomingLabelTrackingCode, outgoingLabelTrackingCode', 'length', 'max'=>50),
			array('paymentMethod, incomingLabelCarrier, outgoingLabelCarrier', 'length', 'max'=>10),
			array('modifyTime', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, customerId, quoteId, quoteDetail, price, status, createTime, modifyTime, paymentMethod, paymentStatus, incomingLabelCarrier, incomingLabelTrackingCode, outgoingLabelCarrier, outgoingLabelTrackingCode', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'quote' => array(self::BELONGS_TO, 'Gadgetquote', 'quoteId'),
			'customer' => array(self::BELONGS_TO, 'Customer', 'customerId'),
			'customerquotelogs' => array(self::HAS_MANY, 'Customerquotelog', 'customerQuoteId'),
			'paymentchecks' => array(self::HAS_MANY, 'Paymentcheck', 'customerQuoteId'),
			'paymentpaypals' => array(self::HAS_MANY, 'Paymentpaypal', 'customerQuoteId'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'customerId' => 'Customer',
			'quoteId' => 'Quote',
			'quoteDetail' => 'Quote Detail',
			'price' => 'Price',
			'status' => 'Status',
			'createTime' => 'Create Time',
			'modifyTime' => 'Modify Time',
			'paymentMethod' => 'Payment Method',
			'paymentStatus' => 'Payment Status',
			'incomingLabelCarrier' => 'Incoming Label Carrier',
			'incomingLabelTrackingCode' => 'Incoming Label Tracking Code',
			'outgoingLabelCarrier' => 'Outgoing Label Carrier',
			'outgoingLabelTrackingCode' => 'Outgoing Label Tracking Code',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		$criteria->together = true;
		$criteria->with = array('customer');
		$criteria->compare('id',$this->id);
		$criteria->compare('customerId',$this->customerId);
		
		//$criteria->order = 'createTime DESC';
		$criteria->addSearchCondition('customer.firstName',$this->customerId,true,'OR');
		$criteria->addSearchCondition('customer.lastName',$this->customerId,true,'OR');
		$criteria->addSearchCondition('customer.email',$this->customerId,true,'OR');
		
		$criteria->compare('quoteId',$this->quoteId);
		$criteria->compare('quoteDetail',$this->quoteDetail,true);
		$criteria->compare('price',$this->price);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('createTime',$this->createTime,true);
		$criteria->compare('modifyTime',$this->modifyTime,true);
		$criteria->compare('paymentMethod',$this->paymentMethod,true);
		$criteria->compare('paymentStatus',$this->paymentStatus,true);
		$criteria->compare('incomingLabelCarrier',$this->incomingLabelCarrier,true);
		$criteria->compare('incomingLabelTrackingCode',$this->incomingLabelTrackingCode,true);
		$criteria->compare('outgoingLabelCarrier',$this->outgoingLabelCarrier,true);
		$criteria->compare('outgoingLabelTrackingCode',$this->outgoingLabelTrackingCode,true);
		return new CActiveDataProvider($this->ordByNewer(), array(
			'criteria'=>$criteria,
		));
	}
	
	public function scopes()
    {
        return array(
            'ordByNewer'=>array(
                'order'=>'modifyTime ASC',
            ),
        );
    }
	
	function getStatus($id){
		$model = $this->findByPk($id);
		if(!empty($model)){
			switch($model->status){
				case 0:
					return Customerquote::ARRSTATUSW;
				break;
				case 1:
					return Customerquote::ARRSTATUSR;
				break;
				case 2:
					return Customerquote::ARRSTATUSA;
				break;
				case 3:
					return Customerquote::ARRSTATUSC;
				break;
			}
		}else{
			return 0;
		}
	}
	function getListStatus(){
		return array(
			0=>Customerquote::ARRSTATUSW,
			1=>Customerquote::ARRSTATUSR,
			2=>Customerquote::ARRSTATUSA,
			3=>Customerquote::ARRSTATUSC,
		);
	}
}